<?php

namespace App\Http\Controllers\Home;

use App\Model\Business_group;
use App\Model\Company;
use App\Model\Department;
use App\Model\Group_member;
use App\Model\Staff;
use App\Model\Users;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
//业务组管理
class Business_groupController extends CommonController
{
    //
	protected $group;
	protected $department;
	protected $group_member;
	protected $Staff;
	protected $Users;
	protected  static $address;
	public function __construct()
	{
		parent::__construct();
		$this->group=new Business_group();
		$this->department=new Department();
		$this->group_member=new Group_member();
		$this->Staff=new Staff();
		$this->Users=new Users();
		self::$address=Company::where(['id'=>$this->company_id])->first()->address;

	}


//	业务组列表
	public function group_list(Request $request){
		$where=[];
		$where['name']=isset($request->name)?$request->name:null;
		$where['start_time']=isset($request->start_time)?$request->start_time:null;
		$where['end_time']=isset($request->end_time)?$request->end_time:null;
		$where['leader_name']=isset($request->leader_name)?$request->leader_name:null;
		try{
			$data=$this->group->allgroupData($where);
		}catch (\Exception $exception){
			$message=$exception->getMessage();
		}
		if(!isset($message)){
			return $this->success($data);
		}else{
			return $this->failed($message);
		}
	}


//	新增业务组
	public function add_group(){
//		$where=[];
//		$alldepartment=$this->department->allDepartment($where);
		return 1;
	}


//	保存业务组
	public function save_group(Request $request){
//		模拟数据
//		$data=[
//			'name'=>'10010',
//			'department_id'=>1,
//			'leader'=>1,
//			'leader_name'=>'张三',
//			'desc'=>[
//				[
//					'uid'=>1,
//					'department'=>'徐汇店',
//					'realname'=>'张三',
//					'roles'=>'管理员',
//					'address'=>'上海嘉定',
//				],
//				[
//					'uid'=>2,
//					'department'=>'张江店',
//					'realname'=>'李四',
//					'roles'=>'超级管理员',
//					'address'=>'上海闵行',
//				]
//			],
//		];
//		模拟数据结束
		$data=$request->input();
		$groupData['name']=$data['name'];
		$groupData['department_id']=$data['department_id'];
		$groupData['leader']=$data['leader'];
		$users=new Users();
		$leader_name=$users->usersInfo($groupData['leader']);
		$groupData['leader_name']=$leader_name;
		$groupData['company_id']=$this->company_id;
		$userData=$data['desc'];
		DB::beginTransaction();
		try{
			$group_id=$this->group->create($groupData)->id;
			$this->group_member=[];
			foreach($userData as $kk=>$vv){
				$newArr[$kk]['group_id']=$group_id;
				$newArr[$kk]['user_id']=$vv['uid'];
				$newArr[$kk]['department_name']=$vv['department'];
				$newArr[$kk]['department_id']=Department::where(['name'=>$vv['department']])->first()->id;
				$newArr[$kk]['realname']=$vv['realname'];
				$newArr[$kk]['role_name']=$vv['roles'];
				$newArr[$kk]['address']=$vv['address'];
				$newArr[$kk]['company_id']=$this->company_id;
			}
			DB::table('group_member')->insert($newArr);
			DB::commit();
		}catch (\Exception $exception){
			$message=$exception->getMessage();
			DB::rollBack();
		}
		if(!isset($message)){
			return $this->stored(['code'=>200,'message'=>'添加成功']);
		}else{
			return $this->failed($message);
		}

	}


//	业务组编辑
	public function group_edit(Request $request){
		$id=$request->id;
		try{
			$data=$this->group->desc($id);
			$newArr=[];
			foreach($data['member'] as $kk=>$vv){
				$newArr['leader_name']=$data['leader_name'];
				$newArr['name']=$data['name'];
				$newArr['department_id']=$data['department_id'];
				$newArr['leader']=$data['leader'];
				$newArr['desc'][$kk]['uid']=$vv['user_id'];
				$newArr['desc'][$kk]['roles']=$vv['role_name'];
				$newArr['desc'][$kk]['address']=$vv['address'];
				$newArr['desc'][$kk]['realname']=$vv['realname'];
				$newArr['desc'][$kk]['department']=$vv['department_name'];
			}
		}catch (\Exception $exception){
			$message=$exception->getMessage();
		}
		if(!isset($message)){
			return $this->success($newArr);
		}else{
			return $this->failed($message);
		}
	}

//	业务组编辑保存
	public function edit_save(Request $request){
//		$data=[
//			'id'=>13,
//			'name'=>'10020',
//			'department_id'=>1,
//			'leader'=>1,
//			'desc'=>[
//				[
//					'user_id'=>1,
//					'department_name'=>'徐汇店',
//					'realname'=>'张三',
//					'role_name'=>'管理员',
//					'address'=>'上海嘉定',
//				],
//				[
//					'user_id'=>2,
//					'department_name'=>'张江店',
//					'realname'=>'李四',
//					'role_name'=>'超级管理员',
//					'address'=>'上海闵行',
//				]
//			],
//		];
		$data=$request->input();
		foreach($data['desc'] as  $kk=>$vv){
			$data['desc'][$kk]['group_id']=$data['id'];
		}
		$bussess['name']=$data['name'];
		$bussess['department_id']=$data['department_id'];
		$bussess['leader']=$data['leader'];
		$users=new Users();
		$bussess['leader_name']=$users->usersInfo($bussess['leader']);
		$userData=$data['desc'];
		$newArr=[];
		foreach($userData as $kk=>$vv){
			$newArr[$kk]['group_id']=$data['id'];
			$newArr[$kk]['user_id']=$vv['uid'];
			$newArr[$kk]['department_name']=$vv['department'];
			$newArr[$kk]['department_id']=Department::where(['name'=>$vv['department']])->first()->id;
			$newArr[$kk]['realname']=$vv['realname'];
			$newArr[$kk]['role_name']=$vv['roles'];
			$newArr[$kk]['address']=$vv['address'];
			$newArr[$kk]['company_id']=$this->company_id;
		}
		DB::beginTransaction();
		try{
			$result=$this->group->where(['id'=>$data['id']])->update($bussess);
			$this->group_member->where(['group_id'=>$data['id']])->delete();
			DB::table('group_member')->insert($newArr);
			DB::commit();
		}catch (\Exception $exception){
			$message=$exception->getMessage();
			DB::rollBack();
		}
		if(!isset($message)){
			return $this->success($result);
		}else{
			return $this->failed($message);
		}
	}
//	搜索添加业务组成员
	public function add_group_member(Request $request){
		$department_name=isset($request->department_name)?$request->department_name:null;
		$data=$this->Staff->department_staff_($department_name);
		return $this->success($data);
	}
//	删除业务组
	public function del_group(Request  $request){
		$id=$request->id;
		try{
			$result=$this->group->where(['id'=>$id])->delete();
		}catch (\Exception $exception){
			$message=$exception->getMessage();
		}
		if(!isset($message)){
			return $this->success($result);
		}else{
			return $this->failed($message);
		}
	}


//	业务组负责人
	public function leader_list(Request $request){
		$department_id=$request->department_id;
		try{
			$data=$this->Staff->department_staff($department_id);
		}catch (\Exception $exception){
			$message=$exception->getMessage();
		}
		if(!isset($message)){
			return $this->success($data);
		}else{
			return $this->failed($message);
		}

	}


//	添加成员
	public function leader_lists(Request $request)
	{
		$address=Company::where(['id'=>$this->company_id])->first()->address;
		$department_name = $request->department_name;
		try {
			 $data = $this->Users->department_staffs($department_name,$group_id=null);
			if(!$data){
				return $this->success([]);
			}
			$newArr = [];
			foreach ($data as $kk => $vv) {
				$newArr[$kk]['department_id'] = $vv['departments']['id'];
				$newArr[$kk]['department'] = $vv['departments']['name'];
				$newArr[$kk]['realname'] = $vv['realname'];
				$newArr[$kk]['address'] = $vv['address'];
				$newArr[$kk]['uid'] = $vv['id'];
//				$newArr[$kk]['is_manage']=$vv['is_manage'];
				if($vv['is_manage']==2){
					$newArr[$kk]['address']=$address;
				}else{
					$newArr[$kk]['address'] = $vv['address'];
				}
				foreach ($vv['roles'] as $k => $v) {
					$newArr[$kk]['roles'][] = $v['display_name'];
				}
				if(isset($newArr[$kk]['roles'])){
					$newArr[$kk]['roles'] = implode(',', $newArr[$kk]['roles']);
				}else{
					$newArr[$kk]['roles']=null;
				}
			}
		} catch (\Exception $exception) {
			$message = $exception->getMessage();
		}
		if (!isset($message)) {
			return $this->success($newArr);
		} else {
			return $this->failed($message);
		}

	}
//	public function leader_lists_xiugai(Request $request)
//	{
//		$department_name = $request->department_name;
//		try {
//			$data = $this->Users->department_staffs($department_name,$group_id=null);
//			if(!$data){
//				return $this->success([]);
//			}
//			$newArr = [];
//
//			foreach ($data as $kk => $vv) {
//				$newArr[$kk]['department'] = $vv['departments']['name'];
//				$newArr[$kk]['realname'] = $vv['userinfo']['realname'];
//				$newArr[$kk]['address'] = $vv['address'];
//				$newArr[$kk]['uid'] = $vv['uid'];
//				foreach ($vv['userinfo']['roles'] as $k => $v) {
//					$newArr[$kk]['roles'][] = $v['display_name'];
//				}
//
//				if(isset($newArr[$kk]['roles'])){
//					$newArr[$kk]['roles'] = implode(',', $newArr[$kk]['roles']);
//				}else{
//					$newArr[$kk]['roles']=null;
//				}
//			}
//		} catch (\Exception $exception) {
//			$message = $exception->getMessage();
//		}
//		if (!isset($message)) {
//			return $this->success($newArr);
//		} else {
//			return $this->failed($message);
//		}
//
//	}
//  编辑添加成员
	public function leader_lists_(Request $request)
	{
		$department_name = $request->department_name;
		$group_id = $request->group_id;
		try {
			$data = $this->Staff->department_staffs_($department_name,$group_id);
			if(!$data){
				return $this->success([]);
			}
			$newArr = [];
			foreach ($data as $kk => $vv) {
				$newArr[$kk]['department'] = $vv['departments']['name'];
				$newArr[$kk]['realname'] = $vv['userinfo']['realname'];
				$newArr[$kk]['address'] = $vv['address'];
				$newArr[$kk]['uid'] = $vv['uid'];
				foreach ($vv['userinfo']['roles'] as $k => $v) {
					$newArr[$kk]['roles'][] = $v['display_name'];
				}

				if(isset($newArr[$kk]['roles'])){
					$newArr[$kk]['roles'] = implode(',', $newArr[$kk]['roles']);
				}else{
					$newArr[$kk]['roles']=null;
				}
			}
		} catch (\Exception $exception) {
			$message = $exception->getMessage();
		}
		if (!isset($message)) {
			return $this->success($newArr);
		} else {
			return $this->failed($message);
		}

	}




}
